package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicCommunicationModelDTO;
import com.iwomy.secureplat.platform.entity.BasicAuthorizationRecordEntity;
import com.iwomy.secureplat.platform.entity.BasicCommunicationModelEntity;
import com.iwomy.secureplat.platform.mapper.BasicAuthorizationRecordMapper;
import com.iwomy.secureplat.platform.mapper.BasicCommunicationModelMapper;
import com.iwomy.secureplat.platform.service.BasicCommunicationModelService;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 通讯模型表
 *
 * @author pig
 * @date 2023-11-14 11:42:05
 */
@Service
public class BasicCommunicationModelServiceImpl extends ServiceImpl<BasicCommunicationModelMapper, BasicCommunicationModelEntity> implements BasicCommunicationModelService {

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Autowired
	private BasicAuthorizationRecordMapper authorizationRecord;

	@Override
	public Page getBasicCommunicationModelPage(Page page, BasicCommunicationModelDTO basicCommunicationModelDTO) {

		LambdaQueryWrapper<BasicCommunicationModelEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicCommunicationModelEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> communicationModelId = list.stream().map(BasicCommunicationModelEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(communicationModelId)) return page;

		LambdaQueryWrapper<BasicCommunicationModelEntity> wrapper = Wrappers.lambdaQuery();

		if (StrUtil.isNotEmpty(basicCommunicationModelDTO.getName())) {
			wrapper.like(BasicCommunicationModelEntity::getName, basicCommunicationModelDTO.getName());
		}

		if (ArrayUtil.isNotEmpty(basicCommunicationModelDTO.getCreateTime())) {
			wrapper.ge(BasicCommunicationModelEntity::getCreateTime, basicCommunicationModelDTO.getCreateTime()[0]).le(BasicCommunicationModelEntity::getCreateTime,
					basicCommunicationModelDTO.getCreateTime()[1]);
		}

		wrapper.in(BasicCommunicationModelEntity::getId, communicationModelId);

		return baseMapper.selectPage(page, wrapper);
	}

	@Override
	public List<? extends Object> pageByScope(BasicCommunicationModelEntity basicCommunicationModelEntity) {
		Long parentId = remoteDeptUtils.getParentId(SecurityUtils.getUser().getDeptId());

		List<BasicCommunicationModelEntity> communicationModelEntities = new ArrayList<>();
		//是租户时，产品库为设备列表
		if (parentId.equals(0L)) {
			LambdaQueryWrapper<BasicCommunicationModelEntity> queryWrapper = Wrappers.lambdaQuery();
			queryWrapper.eq(BasicCommunicationModelEntity::getState, 10);
			communicationModelEntities = this.getBaseMapper().selectListByScope(queryWrapper, new DataScope());
		}

		LambdaQueryWrapper<BasicAuthorizationRecordEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(BasicAuthorizationRecordEntity::getDeptId, SecurityUtils.getUser().getDeptId());
		List<BasicAuthorizationRecordEntity> authorizationRecordList = authorizationRecord.selectList(queryWrapper);
		List<Long> communicationModelIds = authorizationRecordList.stream().map(BasicAuthorizationRecordEntity::getCommunicationModelId).collect(Collectors.toList());
		List<BasicCommunicationModelEntity> resultList = new ArrayList<>();
		if (communicationModelIds.size() != 0) {
			//多表查询
			MPJLambdaWrapper<BasicCommunicationModelEntity> wrapper = new MPJLambdaWrapper<>();
			wrapper.selectAll(BasicCommunicationModelEntity.class)
					.eq(BasicCommunicationModelEntity::getState, 10)
					.in(BasicCommunicationModelEntity::getId, communicationModelIds);

			List<BasicCommunicationModelEntity> records = this.getBaseMapper().selectJoinList(BasicCommunicationModelEntity.class, wrapper);

			// 设备类型去重，并转换为BasicCommunicationModelEntity的对象
			Map<Long, BasicCommunicationModelEntity> communicationModelMap = new HashMap<>();
			Set<Long> communicationModels = new HashSet<>(); // 用于存储已经出现过的 communicationModelId

			for (BasicCommunicationModelEntity record : records) {
				Long communicationModelId = record.getId();
				if (!communicationModels.contains(communicationModelId)) {
					communicationModelMap.put(communicationModelId, record);
					communicationModels.add(communicationModelId); // 将 communicationModelId 添加到 Set 中
				}
			}
			// 获取去重后的结果列表
			resultList = new ArrayList<>(communicationModelMap.values());

		}
		if (resultList.isEmpty()) {
			return communicationModelEntities;
		}

		List<Object> combinedList = Stream.of(communicationModelEntities, resultList)
				.flatMap(Collection::stream)
				.collect(Collectors.toList());

		return combinedList;
	}
}